<template>
  <div class="wrapper-list">
    <!-- 数据列表区 -->
    <div class="wrapper-table">
      <el-table
        v-loading="flagLoading"
        :data="tblDataList"
        :header-cell-style="{ 'text-align': 'center' }"
        row-key="id"
        fit
        size="small"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column align="center" label="编号" width="60" prop="id" />

        <el-table-column label="名称" width="300" prop="title" />

        <el-table-column label="备注" prop="remark" />

        <el-table-column align="center" width="80" label="操作">
          <template #default="scope">
            <div class="wrapper-op">
              <a class="ibtn ibtn-green" title="修改" @click="onEdit(scope.row)">
                <el-icon><Edit /></el-icon>
              </a>
              <a class="ibtn ibtn-red" title="删除" @click="onDelete(scope.row.id)">
                <el-icon><Delete /></el-icon>
              </a>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { toastSuccess } from '@/utils/toast'
import { setSStorageObj } from '@/utils/web-storage'

import { ElMessageBox } from 'element-plus'

import router from '@/router'
import store from '@/store'
import { defineComponent, ref, onMounted } from 'vue'
export default defineComponent({
  name: 'DictionaryListAllView',
  setup() {
    const idDictionary = ref(17)
    const tblDataList = ref([])
    const flagLoading = ref(true)

    // 从api获取所有字典条目
    const listAll = () => {
      flagLoading.value = true
      store
        .dispatch('dictionary/listAll')
        .then((response) => {
          flagLoading.value = false
          tblDataList.value = response
        })
        .catch(() => {
          flagLoading.value = false
        })
    }
    // 编辑
    const onEdit = (row) => {
      setSStorageObj('ParamData', row)
      router.push({ path: 'edit' })
    }
    // 删除
    const onDelete = (id) => {
      ElMessageBox.confirm('删除该条目后会同时自动删除该条目所有值，确定要删除该字典吗？', '删除警告', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          store
            .dispatch('dictionary/delById', id)
            .then(() => {
              toastSuccess('删除成功！')
              listAll()
            })
            .catch(() => {})
        })
        .catch(() => {})
    }

    onMounted(() => {
      listAll()
    })

    return {
      idDictionary,
      tblDataList,
      flagLoading,
      listAll,
      onEdit,
      onDelete,
    }
  },
})
</script>

<style lang="scss" scoped></style>
